package com.cskaoyan.exceptionhandler;

import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.vo.NoDataRespVO;
import com.cskaoyan.exception.MarketValidationException;
import com.cskaoyan.exception.UnLoginException;
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

/**
 * 自定义异常处理
 *
 * @author Can
 * @since 2022/06/27 15:39
 */
@RestControllerAdvice
public class ExceptionController {
    // 在形参中可以直接接收你抛出的异常，你映射的是什么异常，就可以以什么类型的形参来接收

    /**
     * 形参进行类型转换时 进行的类型转换异常处理  “abc” -> int 会报异常
     *
     * @param exception
     * @return com.cskaoyan.bean.vo.BaseRespVo
     * @author Can
     * @since 2022/06/27 15:46
     */
    @ExceptionHandler(InvalidFormatException.class)
    public BaseRespVo typeCastExceptionHandler(InvalidFormatException exception) {
        System.out.println(exception.getMessage());
        BaseRespVo baseRespVo = BaseRespVo.invalidParameter("参数值不对");
        baseRespVo.setErrno(402);
        return baseRespVo;
    }

    /**
     * 用于接受数据类型不对
     *
     * @param exception
     * @return com.cskaoyan.bean.vo.BaseRespVo
     * @author: Caspar CXC
     * @since 2022/06/30 21:28
     */
    @ExceptionHandler(MethodArgumentTypeMismatchException.class)
    public BaseRespVo MethodArgumentTypeMismatchExceptionHandler(MethodArgumentTypeMismatchException exception) {
        System.out.println(exception.getMessage());
        BaseRespVo baseRespVo = BaseRespVo.invalidParameter("参数值类型不对");
        baseRespVo.setErrno(401);
        return baseRespVo;
    }

    /**
     * 参数类型格式不对
     *
     * @param exception
     * @return com.cskaoyan.bean.vo.BaseRespVo
     * @author: Caspar CXC
     * @since 2022/06/30 21:31
     */
    @ExceptionHandler(NumberFormatException.class)
    public BaseRespVo NumberFormatExceptionHandler(NumberFormatException exception) {
        System.out.println(exception.getMessage());
        BaseRespVo baseRespVo = BaseRespVo.invalidParameter("参数值类型不对");
        baseRespVo.setErrno(401);
        return baseRespVo;
    }

    @ExceptionHandler(AuthenticationException.class)
    public BaseRespVo authExceptionHandler(AuthenticationException exception) {
        System.out.println(exception.getMessage());
        BaseRespVo baseRespVo = BaseRespVo.invalidParameter("认证失败，用户名或密码错误");
        baseRespVo.setErrno(401);
        return baseRespVo;
    }

    @ExceptionHandler(AuthorizationException.class)
    public BaseRespVo authorizationHandler(AuthorizationException exception) {
        System.out.println(exception.getMessage());
        BaseRespVo baseRespVo = BaseRespVo.invalidParameter("没有权限，拒绝访问");
        baseRespVo.setErrno(401);
        return baseRespVo;
    }

    /**
     * 处理空指针异常 返回登录超时
     *
     * @return
     * @author LC_R-SH
     * @since 2022/06/29 15:51
     */

    // @ExceptionHandler(NullPointerException.class)
    // @ResponseBody
    // public NoDataRespVO nullPointExceptionHandler(NullPointerException exception) {
    //     System.out.println(exception.getMessage());
    //     NoDataRespVO noDataRespVO = new NoDataRespVO();
    //     noDataRespVO.setErrno(501);
    //     noDataRespVO.setErrmsg("未登录");
    //     return noDataRespVO;
    // }


    /**
     * 处理接收到的validated异常 返回格式有误
     *
     * @return
     * @author LC_R-SH
     * @since 2022/06/29 22:32
     */
    @ExceptionHandler(MarketValidationException.class)
    @ResponseBody
    public NoDataRespVO marketValidationExceptionHandler(MarketValidationException exception) {
        System.out.println(exception.getMessage());
        NoDataRespVO noDataRespVO = new NoDataRespVO();
        noDataRespVO.setErrmsg("参数格式有误");
        noDataRespVO.setErrno(401);
        return noDataRespVO;
    }

    /**
     * 未登录异常处理程序
     *
     * @param exception 异常
     * @return {@link NoDataRespVO }
     * @author: jinmo
     * @date: 2022/06/30 22:34:58
     */
    @ExceptionHandler(UnLoginException.class)
    @ResponseBody
    public NoDataRespVO nullPointExceptionHandler(UnLoginException exception) {
        System.out.println(exception.getMessage());
        NoDataRespVO noDataRespVO = new NoDataRespVO();
        noDataRespVO.setErrno(501);
        noDataRespVO.setErrmsg("未登录");
        return noDataRespVO;
    }

   /*测试的异常
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public BaseRespVo exception(Exception e) {
        e.printStackTrace();
        return BaseRespVo.error("");
    }*/

}
